package com.ixuestar.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ixuestar.dao.IxuestarParentsMapper;
import com.ixuestar.dao.SmartTerminalMapper;
import com.ixuestar.entity.MemberPrintRecord;
import com.ixuestar.entity.MemberStudent;
import com.ixuestar.entity.MembershipInfo;
import com.ixuestar.entity.MembershipReRecord;
import com.ixuestar.entity.MembershipRealname;
import com.ixuestar.entity.MembershipVipInfo;
import com.ixuestar.entity.MinxueStu;
import com.ixuestar.entity.RealnameInfo;
import com.ixuestar.entity.RechargeRecord;
import com.ixuestar.entity.School;
import com.ixuestar.entity.SchoolClass;
import com.ixuestar.entity.SchoolGrade;
import com.ixuestar.entity.SchoolStudent;
import com.ixuestar.entity.TopupcardInfo;
import com.ixuestar.service.SmartTerminalService;

@Service("smartTerminalService")
@Transactional
public class SmartTerminalServiceImpl implements SmartTerminalService{
	
	@Autowired
	private SmartTerminalMapper smartTerminalMapper;
	
	@Autowired
	private IxuestarParentsMapper ixuestarParentsMapper;
	
	Logger logger = Logger.getLogger(SmartTerminalServiceImpl.class);
	
	@Override
	public JSONObject getLoginup(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		String userName = jsonObject.getString("userName");
		String passWord = jsonObject.getString("passWord");
		JSONObject jsObject = new JSONObject();
		try {
			//判断用户名的类别
			int userNameLength = userName.length();
			if (userNameLength==10) {
				jsObject = getLoginupByMembership(userName, passWord);
				logger.info("------用户名登陆-----");
			}else if (userNameLength==11) {
				jsObject = getLoginupByMobileNumber(userName, passWord);
				logger.info("------手机号登陆-----");
			}else if (userNameLength==18) {
				jsObject = getLoginupByIdNumber(userName, passWord);
				logger.info("------身份证-----");
			}else if (userNameLength==48){
				jsObject = getLoginupByQrCodeLink(userName, passWord);
				logger.info("------二维码-----");
				
			}else {
				jsObject.put("code", 1);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}

	@Override
	public JSONObject getLoginupByMembership(String userName, String passWord) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		//定义状态
		int code = 0;
		//创建参数容器
		Map<String, Object> hashMap = new HashMap<String, Object>();
		try {
			hashMap.put("lineKey", "ixmNo");
			hashMap.put("lineValue", userName);
			MembershipInfo membershipInfo = smartTerminalMapper.getMembershipInfo(hashMap);
			if (membershipInfo!=null) {
				//判断密码
				String ixmPassword = membershipInfo.getIxmPassword();
				String ixmActivated = membershipInfo.getIxmActivated();
				if (ixmPassword.equals(passWord)) {
					//获取学生和会员卡的绑定关系
					String ixmId = membershipInfo.getIxmId();
					hashMap.put("lineKey", "ixmId");
					hashMap.put("lineValue", ixmId);
					MemberStudent memberStudent = smartTerminalMapper.getMemberStudent(hashMap);
					if (memberStudent!=null) {
						int imvType = 0;
						MembershipVipInfo membershipVipInfo = ixuestarParentsMapper.getMembershipVipInfo(hashMap);
						if (membershipVipInfo!=null) {
							imvType = membershipVipInfo.getImvType();
						}
						JSONObject membershipJsonObject = (JSONObject)JSONObject.toJSON(membershipInfo);
						membershipJsonObject.put("imvType", imvType);
						String ixssId = memberStudent.getIxssId();
						//获取学生的敏学账号
						hashMap.put("lineKey", "ixssId");
						hashMap.put("lineValue", ixssId);
						MinxueStu minxueStu = smartTerminalMapper.getMinxueStu(hashMap);
						JSONObject json = (JSONObject)JSONObject.toJSON(minxueStu);
						json.put("ixmId", ixmId);
						if (ixmActivated.equals("N")) {
							ixmActivated = "Y";
							membershipInfo.setIxmActivated(ixmActivated);
							smartTerminalMapper.updatePassword(membershipInfo);
						}
						//3、修改密码
						//加入到json里
						jsObject.put("code", code);
						jsObject.put("minxueInfo", json);
						jsObject.put("membershipInfo", membershipJsonObject);
					}else {
						code = 3;
						jsObject.put("code", code);
					}
				}else {
					code = 2;
					jsObject.put("code", code);
				}
			}else {
				code = 1;
				jsObject.put("code", code);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}

	@Override
	public JSONObject getLoginupByMobileNumber(String userName, String passWord) {
		// TODO Auto-generated method stub
		JSONObject jsonObject = new JSONObject();
		//定义状态
		int code = 0;
		//创建参数容器
		Map<String, Object> hashMap = new HashMap<String, Object>();
		try {
			
			hashMap.put("lineKey", "ixrTelephone");
			hashMap.put("lineValue", userName);
			RealnameInfo realnameInfo = smartTerminalMapper.getRealnameInfo(hashMap);
			if (realnameInfo!=null) {
				//获取会员卡和实名信息关系
				String ixrId = realnameInfo.getIxrId();
				hashMap.put("lineKey", "ixrId");
				hashMap.put("lineValue", ixrId);
				MembershipRealname membershipRealname = smartTerminalMapper.getMembershipRealname(hashMap);
				String ixmId = membershipRealname.getIxmId();
				//获取会员信息
				hashMap.put("lineKey", "ixmId");
				hashMap.put("lineValue", ixmId);
				MembershipInfo membershipInfo = smartTerminalMapper.getMembershipInfo(hashMap);
				String ixmPassword = membershipInfo.getIxmPassword();
				if (ixmPassword.equals(passWord)) {
					//获取会员信息
					int imvType = 0;
					MembershipVipInfo membershipVipInfo = ixuestarParentsMapper.getMembershipVipInfo(hashMap);
					if (membershipVipInfo!=null) {
						imvType = membershipVipInfo.getImvType();
					}
					JSONObject membershipJsonObject = (JSONObject)JSONObject.toJSON(membershipInfo);
					membershipJsonObject.put("imvType", imvType);
					//获取学生和会员卡的绑定关系
					MemberStudent memberStudent = smartTerminalMapper.getMemberStudent(hashMap);
					String ixssId = memberStudent.getIxssId();
					//获取学生的敏学账号
					hashMap.put("lineKey", "ixssId");
					hashMap.put("lineValue", ixssId);
					MinxueStu minxueStu = smartTerminalMapper.getMinxueStu(hashMap);
					JSONObject json = (JSONObject)JSONObject.toJSON(minxueStu);
					json.put("ixmId", ixmId);
					String ixmActivated = membershipInfo.getIxmActivated();
					if (ixmActivated.equals("N")) {
						ixmActivated = "Y";
						membershipInfo.setIxmActivated(ixmActivated);
						smartTerminalMapper.updatePassword(membershipInfo);
					}
					//加入到json里
					jsonObject.put("code", code);
					jsonObject.put("minxueInfo", json);
					jsonObject.put("membershipInfo", membershipJsonObject);
				}else {
					code = 2;
					jsonObject.put("code", code);
				}
			}else {
				code = 1;
				jsonObject.put("code", code);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsonObject;
	}

	@Override
	public JSONObject getLoginupByIdNumber(String userName, String passWord) {
		// TODO Auto-generated method stub
		JSONObject jsonObject = new JSONObject();
		//定义状态
		int code = 0;
		//创建参数容器
		Map<String, Object> hashMap = new HashMap<String, Object>();
		try {
			
			hashMap.put("lineKey", "ixrIdNumber");
			hashMap.put("lineValue", userName);
			RealnameInfo realnameInfo = smartTerminalMapper.getRealnameInfo(hashMap);
			if (realnameInfo!=null) {
				//获取会员卡和实名信息关系
				String ixrId = realnameInfo.getIxrId();
				hashMap.put("lineKey", "ixrId");
				hashMap.put("lineValue", ixrId);
				MembershipRealname membershipRealname = smartTerminalMapper.getMembershipRealname(hashMap);
				String ixmId = membershipRealname.getIxmId();
				//获取会员信息
				hashMap.put("lineKey", "ixmId");
				hashMap.put("lineValue", ixmId);
				MembershipInfo membershipInfo = smartTerminalMapper.getMembershipInfo(hashMap);
				String ixmPassword = membershipInfo.getIxmPassword();
				if (ixmPassword.equals(passWord)) {
					//获取会员信息
					int imvType = 0;
					MembershipVipInfo membershipVipInfo = ixuestarParentsMapper.getMembershipVipInfo(hashMap);
					if (membershipVipInfo!=null) {
						imvType = membershipVipInfo.getImvType();
					}
					JSONObject membershipJsonObject = (JSONObject)JSONObject.toJSON(membershipInfo);
					membershipJsonObject.put("imvType", imvType);
					//获取学生和会员卡的绑定关系
					MemberStudent memberStudent = smartTerminalMapper.getMemberStudent(hashMap);
					String ixssId = memberStudent.getIxssId();
					//获取学生的敏学账号
					hashMap.put("lineKey", "ixssId");
					hashMap.put("lineValue", ixssId);
					MinxueStu minxueStu = smartTerminalMapper.getMinxueStu(hashMap);
					JSONObject json = (JSONObject)JSONObject.toJSON(minxueStu);
					json.put("ixmId", ixmId);
					String ixmActivated = membershipInfo.getIxmActivated();
					if (ixmActivated.equals("N")) {
						ixmActivated = "Y";
						membershipInfo.setIxmActivated(ixmActivated);
						smartTerminalMapper.updatePassword(membershipInfo);
					}
					//查询爱学思达会员信息
					
					//加入到json里
					jsonObject.put("code", code);
					jsonObject.put("minxueInfo", json);
					jsonObject.put("membershipInfo", membershipJsonObject);
				}else {
					code = 2;
					jsonObject.put("code", code);
				}
			}else {
				code = 1;
				jsonObject.put("code", code);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsonObject;
	}

	@Override
	public JSONObject getLoginupByQrCodeLink(String userName, String passWord) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		//定义状态
		int code = 0;
		//创建参数容器
		Map<String, Object> hashMap = new HashMap<String, Object>();
		try {
			hashMap.put("lineKey", "ixmQrCodeLink");
			hashMap.put("lineValue", userName);
			MembershipInfo membershipInfo = smartTerminalMapper.getMembershipInfo(hashMap);
			logger.info("-------membershipInfo-"+membershipInfo+"-----------");
			if (membershipInfo!=null) {
				String ixmId = membershipInfo.getIxmId();
				String ixmActivated = membershipInfo.getIxmActivated();
				hashMap.put("lineKey", "ixmId");
				hashMap.put("lineValue", ixmId);
				//获取学生和会员卡的绑定关系
				MemberStudent memberStudent = smartTerminalMapper.getMemberStudent(hashMap);
				if (memberStudent!=null ) {
					//获取会员信息
					int imvType = 0;
					MembershipVipInfo membershipVipInfo = ixuestarParentsMapper.getMembershipVipInfo(hashMap);
					if (membershipVipInfo!=null) {
						imvType = membershipVipInfo.getImvType();
					}
					JSONObject membershipJsonObject = (JSONObject)JSONObject.toJSON(membershipInfo);
					membershipJsonObject.put("imvType", imvType);
					String ixssId = memberStudent.getIxssId();
					//获取学生的敏学账号
					hashMap.put("lineKey", "ixssId");
					hashMap.put("lineValue", ixssId);
					MinxueStu minxueStu = smartTerminalMapper.getMinxueStu(hashMap);
					JSONObject json = (JSONObject)JSONObject.toJSON(minxueStu);
					json.put("ixmId", ixmId);
					if (ixmActivated.equals("N")) {
						ixmActivated = "Y";
						membershipInfo.setIxmActivated(ixmActivated);
						smartTerminalMapper.updatePassword(membershipInfo);
					}
					//加入到json里
					jsObject.put("code", code);
					jsObject.put("minxueInfo", minxueStu);
					jsObject.put("membershipInfo", membershipJsonObject);
				}else {
					code = 3;
					jsObject.put("code", code);
				}
			}else {
				code = 1;
				jsObject.put("code", code);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}

	
	@Override
	public void writeMemberStudent(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		try {
			String ixmId = jsonObject.getString("ixmId");
			String ixssId = jsonObject.getString("ixssId");
			//创建关系对象
			MemberStudent memberStudent = new MemberStudent();
			//创建id
			String ixsmId = "ixms" + new Date().getTime() + (int)(Math.random()*10);
			String ixsmCreatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); 
			memberStudent.setIxsmId(ixsmId);
			memberStudent.setIxmId(ixmId);
			memberStudent.setIxssId(ixssId);
			memberStudent.setIxsmCreatetime(ixsmCreatetime);
			smartTerminalMapper.writeMemberStudent(memberStudent);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	@Override
	public JSONObject getSchoolList(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		//创建返回对象
		JSONObject jsObject = new JSONObject();
		try {
			Map<String, Object> hashMap = new HashMap<>();
			List<School> schoolInfo = smartTerminalMapper.getSchoolInfo(hashMap);
			//获取学校个数
			int schoolSize = schoolInfo.size();
			//添加到对象里
			jsObject.put("schoolInfo", schoolInfo);
			jsObject.put("schoolSize", schoolSize);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}
	
	
	@Override
	public JSONObject getSchoolGradeList(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		try {
			//通过学校id获取年级列表
			String ixsId = jsonObject.getString("ixsId");
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ixsId");
			hashMap.put("linevalue", ixsId);
			List<SchoolGrade> schoolGrades = smartTerminalMapper.getSchoolGrades(hashMap);
			//年级列表个数
			int schoolGradeSize = schoolGrades.size();
			//添加到返回对象里
			jsonObject.remove("ixsId");
			jsonObject.put("schoolGrades", schoolGrades);
			jsonObject.put("schoolGradeSize", schoolGradeSize);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsonObject;
	}
	
	@Override
	public JSONObject getSchoolClassList(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		try {
			//通过年级id获取班级列表
			String ixsgId = jsonObject.getString("ixsgId");
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ixsgId");
			hashMap.put("linevalue", ixsgId);
			List<SchoolClass> schoolClasses = smartTerminalMapper.getSchoolClasses(hashMap);
			//班级列表个数
			int schoolClassesSize = schoolClasses.size();
			//添加到返回对象里
			jsonObject.remove("ixsgId");
			jsonObject.put("schoolClasses", schoolClasses);
			jsonObject.put("schoolClassesSize", schoolClassesSize);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsonObject;
	}
	
	@Override
	public JSONObject getSchoolStudentList(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		try {
			//通过班级id获取学生列表
			String ixscId = jsonObject.getString("ixscId");
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ixscId");
			hashMap.put("linevalue", ixscId);
			List<SchoolStudent> schoolStudents = smartTerminalMapper.getSchoolStudents(hashMap);
			//班级列表个数
			int schoolStudentsSize = schoolStudents.size();
			//添加到返回对象里
			jsonObject.remove("ixscId");
			jsonObject.put("schoolStudents", schoolStudents);
			jsonObject.put("schoolStudentsSize", schoolStudentsSize);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsonObject;
	}
	
	@Override
	public void updatePassword(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		try {
			String userName = jsonObject.getString("userName");
			String passWord = jsonObject.getString("passWord");
			//修改密码
			Map<String, Object> hashMap = new HashMap<>();
			//1、首先查询用户信息
			hashMap.put("lineKey", "ixmId");
			hashMap.put("lineValue", userName);
			MembershipInfo membershipInfo = smartTerminalMapper.getMembershipInfo(hashMap);
			if (membershipInfo!=null) {
				//2、判断是否激活
				String ixmActivated = membershipInfo.getIxmActivated();
				if (ixmActivated.equals("N")) {
					membershipInfo.setIxmActivated("Y");
				}
				membershipInfo.setIxmPassword(passWord);
				//3、修改密码
				smartTerminalMapper.updatePassword(membershipInfo);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	@Override
	public JSONObject useRechargeCard(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		int code = 0;
		try {
			String ixmId = jsonObject.getString("ixmId");
			String ixtPassword = jsonObject.getString("ixtPassword");
			//充值
			Map<String, Object> hashMap = new HashMap<>();
			//1、查询用户名的有效性
			hashMap.put("lineKey", "ixmId");
			hashMap.put("lineValue", ixmId);
			MembershipInfo membershipInfo = smartTerminalMapper.getMembershipInfo(hashMap);
			if (membershipInfo!=null) {
				//2、首先查询充值卡的有效性
				hashMap.put("lineKey", "ixtPassword");
				hashMap.put("lineValue", ixtPassword);
				TopupcardInfo topupcardInfo = smartTerminalMapper.getTopupcardInfo(hashMap);
				if (topupcardInfo!=null) {
					String ixtActivated = topupcardInfo.getIxtActivated();
					if (ixtActivated.equals("N")) {
						//判断是否够资格成为会员
						double atotal = 0.00;
						hashMap.put("lineKey", "ixmId");
						hashMap.put("lineValue", ixmId);
						List<MembershipReRecord> membershipReRecords = smartTerminalMapper.getMembershipReRecords(hashMap);
						for (int i = 0; i < membershipReRecords.size(); i++) {
							//获取充值记录对象
							MembershipReRecord membershipReRecord = membershipReRecords.get(i);
							String ixeId = membershipReRecord.getIxeId();
							hashMap.put("lineKey", "ixeId");
							hashMap.put("lineValue", ixeId);
							RechargeRecord rechargeRecords = smartTerminalMapper.getRechargeRecords(hashMap);
							atotal = atotal + rechargeRecords.getIxeAmount();
						}
						if (atotal >= 500.00) {
							//创建vip信息
							MembershipVipInfo membershipVipInfo = new MembershipVipInfo();
							String imvId = "imv" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + (int)(Math.random()*10);
							String imvCreatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
							membershipVipInfo.setImvId(imvId);
							membershipVipInfo.setIxmId(ixmId);
							membershipVipInfo.setImvType(1);;
							membershipVipInfo.setImvCreatetime(imvCreatetime);
							ixuestarParentsMapper.writeMembershipVipInfo(membershipVipInfo);
						}else {
							//3、如果有效，写入会员表金额
							double ixtAmount = topupcardInfo.getIxtAmount()*10;
							double ixmAmount = membershipInfo.getIxmAmount();
							double total = ixtAmount + ixmAmount;
							membershipInfo.setIxmAmount(total);
							smartTerminalMapper.updatePassword(membershipInfo);
							String ixePayTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
							//4、写入充值记录
							RechargeRecord rechargeRecord = new RechargeRecord();
							String ixtId = topupcardInfo.getIxtId();
							//创建id
							String ixeId = "ixe" + new Date().getTime() + (int)(Math.random()*10);
							String ixeCreatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
							rechargeRecord.setIxeId(ixeId);
							rechargeRecord.setIxeAmount(ixtAmount);
							rechargeRecord.setIxeTransactionNumber(ixtId);
							rechargeRecord.setIxeRechargeTime(ixePayTime);
							rechargeRecord.setIxeRechargeMethod("充值卡支付");
							rechargeRecord.setIxeCreatetime(ixeCreatetime);
							smartTerminalMapper.writeRechargeRecord(rechargeRecord);
							//5、更改充值卡状态
							topupcardInfo.setIxtActivated("Y");
							smartTerminalMapper.updateTopupcardInfo(topupcardInfo);
							//6、写入充值关系表
							MembershipReRecord membershipReRecord = new MembershipReRecord();
							//创建id和时间
							String ixmrId =  "ixmr" + new Date().getTime() + (int)(Math.random()*10);
							String ixmrCreatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
							membershipReRecord.setIxmrId(ixmrId);
							membershipReRecord.setIxmId(ixmId);
							membershipReRecord.setIxeId(ixeId);
							membershipReRecord.setIxmrCreatetime(ixmrCreatetime);
							smartTerminalMapper.writeMembershipReRecord(membershipReRecord);
						}
						//添加参数
						jsObject.put("code", code);
						jsObject.put("topupcardInfo", topupcardInfo);
					}else {
						code = 2;
						jsObject.put("code", code);
					}
				}else {
					//6、如果验证码无效则返回给用户
					code = 2;
					jsObject.put("code", code);
				}
			}else {
				code = 1;
				jsObject.put("code", code);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}
	
	@Override
	public void writeRealnameInfo(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		try {
			String ixmId = jsonObject.getString("ixmId");
			String ixrName = jsonObject.getString("ixrName");
			String ixrIdNumber = jsonObject.getString("ixrIdNumber");
			String ixrTelephone = jsonObject.getString("ixrTelephone");
			//1、写入实名身份信息
			RealnameInfo realnameInfo = new RealnameInfo();
			//创建id
			String ixrId =  "ixr" + new Date().getTime() + (int)(Math.random()*10);
			String ixrCreatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			realnameInfo.setIxrId(ixrId);
			realnameInfo.setIxrName(ixrName);
			realnameInfo.setIxrIdNumber(ixrIdNumber);
			realnameInfo.setIxrTelephone(ixrTelephone);
			realnameInfo.setIxrCreatetime(ixrCreatetime);
			//2、写入实名和会员关系表
			MembershipRealname membershipRealname = new MembershipRealname();
			String ixmrId =  "ixmr" + new Date().getTime() + (int)(Math.random()*10);
			String ixmrCreatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			membershipRealname.setIxmrId(ixmrId);
			membershipRealname.setIxmId(ixmId);
			membershipRealname.setIxrId(ixrId);
			membershipRealname.setIxmrCreatetime(ixmrCreatetime);
			smartTerminalMapper.writeMembershipRealname(membershipRealname);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	@Override
	public JSONObject deductIxuestar(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		//定义状态
		int code = 0;
		JSONObject memberPrintRecords = new JSONObject();
		//创建参数容器
		Map<String, Object> hashMap = new HashMap<String, Object>();
		try {
			String ixmId = jsonObject.getString("ixmId");
			String ixueb = jsonObject.getString("ixueb");
			//执行参数
			double parseInt = Double.parseDouble(ixueb);
			//查询剩余量
			hashMap.put("lineKey", "ixmId");
			hashMap.put("lineValue", ixmId);
			MembershipInfo membershipInfo = smartTerminalMapper.getMembershipInfo(hashMap);
			if (membershipInfo!=null) {
//				MembershipVipInfo membershipVipInfo = ixuestarParentsMapper.getMembershipVipInfo(hashMap);
//				if (membershipVipInfo!=null) {
//					memberPrintRecords.putAll(getMemberPrintRecords(jsonObject));
//				}
				double ixmAmount = membershipInfo.getIxmAmount();
				//比较是否能够完成本次打印
				double surplus = ixmAmount - parseInt;
				if (surplus<0) {
					code = 1;
				}else {
					membershipInfo.setIxmAmount(surplus);
					smartTerminalMapper.updatePassword(membershipInfo);
				}
			}
			memberPrintRecords.put("code", code);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return memberPrintRecords;
	}
	
	@Override
	public JSONObject getIxuestarDou(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		//创建参数容器
		Map<String, Object> hashMap = new HashMap<String, Object>();
		try {
			String ixmId = jsonObject.getString("ixmId");
			//查询剩余量
			hashMap.put("lineKey", "ixmId");
			hashMap.put("lineValue", ixmId);
			MembershipInfo membershipInfo = smartTerminalMapper.getMembershipInfo(hashMap);
			//获取参数
			if (membershipInfo!=null) {
				double ixmAmount = membershipInfo.getIxmAmount();
				jsonObject.put("ixmAmount", ixmAmount);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return jsonObject;
	}
	
	@Override
	public MembershipInfo getMembershipInfo(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return smartTerminalMapper.getMembershipInfo(map);
	}
	
	@Override
	public JSONObject getMemberStudentInfo(String ixmNo) {
		// TODO Auto-generated method stub
		JSONObject jsonObject = new JSONObject();
		try {
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("lineKey", "ixmNo");
			hashMap.put("lineValue", ixmNo);
			MembershipInfo membershipInfo = smartTerminalMapper.getMembershipInfo(hashMap);
			if (membershipInfo!=null) {
				String ixmId = membershipInfo.getIxmId();
				jsonObject.put("ixmId", ixmId);
				hashMap.put("lineKey", "ixmId");
				hashMap.put("lineValue", ixmId);
				//获取
				MemberStudent memberStudent = smartTerminalMapper.getMemberStudent(hashMap);
				//获取学生表信息
				final String ixssId = memberStudent.getIxssId();
				hashMap.put("lineKey", "ixssId");
				hashMap.put("lineValue", ixssId);
				List<SchoolStudent> schoolStudents = smartTerminalMapper.getSchoolStudents(hashMap);
				if (schoolStudents.size()!=0) {
					SchoolStudent schoolStudent = schoolStudents.get(0);
					String ixssName = schoolStudent.getIxssName();
					jsonObject.put("ixssName", ixssName);
					//获取班级id
					String ixscId = schoolStudent.getIxscId();
					//获取班级
					hashMap.put("lineKey", "ixscId");
					hashMap.put("lineValue", ixscId);
					List<SchoolClass> schoolClasses = smartTerminalMapper.getSchoolClasses(hashMap);
					if (schoolClasses.size()!=0) {
						SchoolClass schoolClass = schoolClasses.get(0);
						String ixscName = schoolClass.getIxscName();
						jsonObject.put("ixscName", ixscName);
						//获取年级名称
						String ixsgId = schoolClass.getIxsgId();
						hashMap.put("lineKey", "ixsgId");
						hashMap.put("lineValue", ixsgId);
						List<SchoolGrade> schoolGrades = smartTerminalMapper.getSchoolGrades(hashMap);
						if (schoolGrades.size()!=0) {
							SchoolGrade schoolGrade = schoolGrades.get(0);
							String ixsgName = schoolGrade.getIxsgName();
							jsonObject.put("ixsgName", ixsgName);
							//获取学校id
							String ixsId = schoolGrade.getIxsId();
							hashMap.put("lineKey", "ixsId");
							hashMap.put("lineValue", ixsId);
							List<School> schoolInfo = smartTerminalMapper.getSchoolInfo(hashMap);
							if (schoolInfo.size()!=0) {
								School school = schoolInfo.get(0);
								String ixsName = school.getIxsName();
								jsonObject.put("ixsName", ixsName);
							}
						}
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsonObject;
	}
	
	@Override
	public JSONObject writeMemberPrintRecord(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		try {
			jsonObject.remove("ixueb");
			//创建打印信息id
			String imprId = "impr" + new Date().getTime() + (int)(Math.random()*10);
			//创建打印时间
			String imprCreatetime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			//添加到对象当中
			jsonObject.put("imprId", imprId);
			jsonObject.put("imprCreatetime", imprCreatetime);
			//转换对象
			MemberPrintRecord memberPrintRecord = JSON.parseObject(jsonObject.toJSONString(), MemberPrintRecord.class);
			smartTerminalMapper.writeMembershipPrint(memberPrintRecord);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsonObject;
	}
	
	@Override
	public JSONObject getMemberPrintRecords(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		JSONObject jsObject = new JSONObject();
		int code = 0;
		try {
			JSONArray jsonArray = new JSONArray();
			//获取参数
			String ixmId = jsonObject.getString("ixmId");
			String impFileId = jsonObject.getString("impFileId");
			//获取记录
			Map<String, Object> hashMap = new HashMap<>();
			hashMap.put("flag", 1);
			hashMap.put("ixmId", ixmId);
			hashMap.put("impFileId", impFileId);
			List<MemberPrintRecord> memberPrintRecords = smartTerminalMapper.getMemberPrintRecords(hashMap);
			if (memberPrintRecords.size()!=0) {
				for (int i = 0; i < memberPrintRecords.size(); i++) {
					MemberPrintRecord memberPrintRecord = memberPrintRecords.get(i);
					if (memberPrintRecord!=null) {
						code = 1;
						jsonArray.add(memberPrintRecord);
					}
				}
			}
			jsObject.put("code", code);
			jsObject.put("memberPrintRecord", jsonArray);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return jsObject;
	}
}
